package com.china.zhuoyue.api.business.area

import androidx.activity.ComponentActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.viewModelScope
import com.china.zhuoyue.api.core.*
import com.china.zhuoyue.api.core.ConvertInterceptor.Companion.JSON_CONTENT_TYPE_HEADER
import kotlinx.coroutines.Job
import retrofit2.http.*

val BUSINESS_AREA_API_SERVICE: BusinessAreaApiService get() = ApiHelper.create(BusinessAreaApiService::class.java)
fun <T> ComponentActivity.launchUIBusinessArea(
    block: suspend BusinessAreaApiService.() -> T,
    apiObserver: Observer<ApiProcess<T>>? = null,
    error: ApiError? = null,
    complete: ApiComplete? = null
): Job = ApiRequest(lifecycleScope, BUSINESS_AREA_API_SERVICE).launchUI(block, error, complete, apiObserver)

fun <T> Fragment.launchUIBusinessArea(
    block: suspend BusinessAreaApiService.() -> T,
    apiObserver: Observer<ApiProcess<T>>? = null,
    error: ApiError? = null,
    complete: ApiComplete? = null,
): Job = ApiRequest(lifecycleScope, BUSINESS_AREA_API_SERVICE).launchUI(block, error, complete, apiObserver)

fun <T> ViewModel.launchUIBusinessArea(
    block: suspend BusinessAreaApiService.() -> T,
    apiObserver: Observer<ApiProcess<T>>? = null,
    error: ApiError? = null,
    complete: ApiComplete? = null
): Job = ApiRequest(viewModelScope, BUSINESS_AREA_API_SERVICE).launchUI(block, error, complete, apiObserver)

interface BusinessAreaApiService {

    /**
     * 商圈首页
     * latitude	纬度		false string
     * longitude	经度		false string
     */
    @POST("/api/busicircle/index")
    @Headers(JSON_CONTENT_TYPE_HEADER)
    @FormUrlEncoded
    suspend fun busicircleIndex(
        @Query("limit") limit: Int,
        @Query("page") page: Int,
        @Field("latitude") latitude: Double? = null,
        @Field("longitude") longitude: Double? = null,
    ): MutableList<BusICircle>?

    /**
     * 活动详情
     * activityId	query	true
     */
    @GET("/api/busicircle/activity")
    suspend fun busicircleActivity(
        @Query("activityId") activityId: Int,
    ): BusICircle?

}